package com.example.scaffolding.util

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import com.bumptech.glide.Glide
import java.io.File
import java.io.OutputStream

object ImageLoader {
    private const val TAG = "ImageLoader"
    fun load(context: Context, filePath: String): String {
        val bitmap: Bitmap = Glide.with(context).asBitmap().load(filePath)
            .into(200, 200).get()

        Log.d(TAG, "load: ${bitmap.toString()}")
        val targetFilePath = context.filesDir.toString() + File.separator + System.currentTimeMillis()
        saveImage(targetFilePath, bitmap)
        return targetFilePath
    }

    fun saveImage(path: String, bitmap: Bitmap) {
        try {
            val file = File(path)
            //outputStream获取文件的输出流对象
            val fos: OutputStream = file.outputStream()
            //压缩格式为JPEG图像，压缩质量为80%
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos)
            fos.flush()
            fos.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
}